package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.brand.bo.BrandCreateBo;
import com.cskaoyan.bean.brand.bo.BrandDeleteBo;
import com.cskaoyan.bean.brand.bo.BrandUpdateBo;
import com.cskaoyan.bean.brand.vo.BrandListVo;
import com.cskaoyan.bean.brand.vo.WXBrand;
import com.cskaoyan.bean.brand.vo.WXBrandListVo;
import com.cskaoyan.mapper.BrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.lang.System;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;


/**
 * @author wangdezhao
 * @date 2021/6/5
 * @description 品牌制造商
 */
@Service
public class BrandServiceImpl implements BrandService{
    @Autowired
    BrandMapper brandMapper;

    /**
     *
     * @param baseReq
     * @param name
     * @param id
     * @return 输出所有品牌信息
     */
    @Override
    public BrandListVo getBrandList(BaseReq baseReq, String name, Integer id) {
        BrandListVo brandListVo = new BrandListVo();

        Integer page = baseReq.getPage();
        Integer limit = baseReq.getLimit();
        String order = baseReq.getOrder();
        String sort = baseReq.getSort();

        PageHelper.startPage(page,limit);//调用分页插件,查询时会进行拼接
        //排序信息
        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause(sort + " " + order);
        BrandExample.Criteria criteria = brandExample.createCriteria();
        if (name !=null && !"".equals(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if (id != null){
            criteria.andIdEqualTo(id);
        }
        // 假删除 ,为false的显示,为true的不显示
        criteria.andDeletedEqualTo(false);

        List<Brand> brands = brandMapper.selectByExample(brandExample);

        //获得分页信息
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();
        // brandVo赋值
        brandListVo.setTotal(total);
        brandListVo.setItems(brands);
        return brandListVo;
    }

    /**
     *  生成品牌
     */
    @Override
    public Integer createBrand(BrandCreateBo brandCreateBo) {
        Brand brand = new Brand();

        brand.setName(brandCreateBo.getName());
        brand.setDesc(brandCreateBo.getDesc());
        brand.setPicUrl(brandCreateBo.getPicUrl());
        brand.setFloorPrice(new BigDecimal(brandCreateBo.getFloorPrice()));
        brand.setAddTime(new Timestamp(System.currentTimeMillis()));
        brand.setDeleted(false);

        int effectedRows = brandMapper.insertSelective(brand);
        return effectedRows;
    }

    /**
     * 修改品牌
     */
    @Override
    public Integer updateBrand(BrandUpdateBo updateBo) {
        Brand brand = new Brand();
        brand.setName(updateBo.getName());
        brand.setDesc(updateBo.getDesc());
        brand.setPicUrl(updateBo.getPicUrl());
        brand.setFloorPrice(new BigDecimal(updateBo.getFloorPrice()));
        brand.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        brand.setDeleted(false);
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andIdEqualTo(updateBo.getId());
        int effectedRows = brandMapper.updateByExampleSelective(brand,brandExample);
        return effectedRows;
    }

    /**
     * 删除brand , 假删除
     * @param deleteBo
     * @return
     */
    @Override
    public Integer deleteBrand(BrandDeleteBo deleteBo) {
        Integer effectedRows = brandMapper.deleteBrandById(deleteBo.getId());
        return effectedRows;
    }


    /**
     * wx 查询品牌信息
     * @param page
     * @param size
     * @return
     */
    @Override
    public WXBrandListVo getWXBrandList(int page, int size) {
        WXBrandListVo wxBrandListVo = new WXBrandListVo();

        PageHelper.startPage(page,size);//调用分页插件,查询时会进行拼接
        //排序信息
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        // 假删除 ,为false的显示,为true的不显示
        criteria.andDeletedEqualTo(false);

        List<Brand> brands = brandMapper.selectByExample(brandExample);
        List<WXBrand> wxBrandList = new ArrayList<>();
        for (Brand brand : brands) {
            WXBrand wxBrand = new WXBrand();
            wxBrand.setId(brand.getId());
            wxBrand.setName(brand.getName());
            wxBrand.setPicUrl(brand.getPicUrl());
            wxBrand.setDesc(brand.getDesc());
            wxBrand.setFloorPrice(brand.getFloorPrice());
            wxBrandList.add(wxBrand);
        }

        //获得分页信息
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();
        // brandVo赋值
        wxBrandListVo.setTotalPages(total);
        wxBrandListVo.setBrandList(wxBrandList);
        return wxBrandListVo;
    }

    @Override
    public Brand getSingleWXBrandVo(int id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    @Override
    public Brand getWXBrandDetail(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }
}
